一樣,對比前面的match檔,現在新增一個bank的json檔案
回到bot檔,現在我們必須撰寫2個部分,一個是讀取,一個是確認你的這個key是否存在......
# cogs/includebanksystem.py
async def open_account(user):
pass
# 省略
async def get_bank_data():
pass
async def open_account(user):
users = await get_bank_data()
if str(user.id) in users:
return False
else:
# 省略
with open("bank.json", "w") as f:
json.dump(users, f)
return True
async def get_bank_data():
with open("bank.json", "r")as f:
users = json.load(f)
return users
users[str(user.id)] = {}
users[str(user.id)]["wallet"] = 0
users[str(user.id)]["bank"] = 0
{
}
# 略
@commands.command()
async def money(self, ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
wallet = users[str(user.id)]["wallet"]
bank = users[str(user.id)]["bank"]
em = discord.Embed(title = f"{ctx.author.name}'s 錢包", color = discord.Color.red())
em.set_thumbnail(url = user.avatar_url)
em.add_field(name = "Wallet", value = wallet)
em.add_field(name = "Bank", value = bank)
await ctx.send(embed = em)
# 略
shop = [
{"name": "Watch", "price": 8},
{"name": "Laptop", "price": 87},
{"name": "PC", "price": 870},
{"name": "PS5", "price": 690},
{"name": "Cat", "price": 1000}
]
@commands.command()
async def market(self, ctx):
pass
em = discord.Embed(title = "market", color = 0x6345)
em.set_thumbnail(url = "https://www.formula-ai.com/wp-content/uploads/2020/09/python_or_java_meme.jpg")
for item in shop:
name = item["name"]
price = item["price"]
em.add_field(name = name, value = f"${price}")
await ctx.send(embed = em)